#include "keeptrack.h"

KeepTrack::KeepTrack()
{
	
	if(isfileEmpty())
	{
		//cout << "nothing in file. writing.......";
		for(int i=0; i<held_scores; ++i)
		{
			hold_name[i]= "Your name here";
			hold_score[i]=0;
			
		}
		writeInfo();
	}
	
}
void KeepTrack::setName(string name1)//,int score1
{
	hold_name[0] = name1;
}

void KeepTrack::setName(string name1, string name2)//,int score1,int score2
{
	hold_name[0] = name1;
	hold_name[1] = name2;
}

void KeepTrack::setScore(int score1)//,int score1
{
	hold_score[0] = score1;
	writeInfo();
}

void KeepTrack::setScore(int score1, int score2)//,int score1,int score2
{
	hold_score[0] = score1;
	hold_score[1] = score2;
	writeInfo();
}

void KeepTrack::writeInfo()
{
	ofstream myfile;
	myfile.open("names.txt");
	for(int i=0; i< held_scores; ++i)
	{
		myfile << hold_name[i] <<endl;
	}
	myfile.close();


	ofstream somefile;
	somefile.open("scores.txt");
	for(int i=0; i< held_scores; ++i)
	{
		somefile << hold_score[i] <<endl;
	}
	somefile.close();

}
void KeepTrack::printInfo()
{
	ifstream myfile("names.txt");
	if(myfile.is_open())
	{
		int count = 0;
		while(!myfile.eof())
		{
			getline(myfile, line);
			hold_name[count]=line;
			count++;
			
		}
		myfile.close();

		ifstream somefile("scores.txt");
		if(somefile.is_open())
		{
			count = 0;
			while(!somefile.eof())
			{
				somefile>>num;
				hold_score[count]=num;
				count++;
			}
			somefile.close();
		}
		for(int i=0; i<held_scores; i++)
		{
			cout << hold_name[i] << " " <<hold_score[i] <<endl;
		}
	}
	else
		cout << "Unable to find scores"<<endl;
	
	
}

bool KeepTrack::isfileEmpty()
{
	
	ifstream filename("names.txt");
	getline(filename,line);
	if(line.empty())
	{
		return true;
		//cout << "there is something in file" <<endl;
	}
	else
	{
		return false;
	}
	filename.close();
}
